//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package xin.fallen.dao;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.collections4.map.ListOrderedMap;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xin.fallen.bean.ConfigRowBean;
import xin.fallen.session.Sessions;
import xin.fallen.util.MyBatisUtil;

public class LocalDataDao {
    private static LocalDataDao dao = null;
    private Logger log = LoggerFactory.getLogger(this.getClass().getName());
    private ListOrderedMap<String, SqlSessionFactory> sessionMap = new ListOrderedMap();

    public static synchronized LocalDataDao getInstance() {
        if(dao == null) {
            dao = new LocalDataDao();
        }

        return dao;
    }

//    public LocalDataDao() {
//        StringReader reader = null;
//
//        try {
//            reader = new StringReader(ConfigDao.getInstance().decodeConnectToString());
//            Iterator var3 = Sessions.listRow.iterator();
//
//            while(var3.hasNext()) {
//                ConfigRowBean e = (ConfigRowBean)var3.next();
//                if(e.getType().compareToIgnoreCase("password") == 0 && e.getEnable().equalsIgnoreCase("1")) {
//                    SqlSessionFactory session = null;
//                    session = (new SqlSessionFactoryBuilder()).build(reader, e.getId());
//                    this.sessionMap.put(e.getId(), session);
//                }
//            }
//        } catch (Exception var13) {
//            this.log.warn("构造异常-{}", var13.getMessage());
//        } finally {
//            try {
//                if(reader != null) {
//                    reader.close();
//                }
//            } catch (Exception var12) {
//                this.log.warn("释放异常-{}", var12.getMessage());
//            }
//
//        }
//
//    }

    public String selectSysTimestamp(String id, String vsiOracleDateFormat) {
        String res = "W:NULL";
        SqlSession sessionTemp = null;

        try {
            SqlSessionFactory e = (SqlSessionFactory)this.sessionMap.get(id);
            sessionTemp = e.openSession();
            res = (String)sessionTemp.selectOne("selectSysTimestamp", vsiOracleDateFormat);
        } catch (Exception var9) {
            res = "E:" + var9.getMessage();
        } finally {
            if(sessionTemp != null) {
                sessionTemp.close();
            }

        }

        return res;
    }

    public String listQueueSQL(String id) {
        String res = "";

        try {
            SqlSession sessionTemp = null;

            try {
                SqlSessionFactory e = (SqlSessionFactory)this.sessionMap.get(id);
                sessionTemp = e.openSession();
                HashMap map = new HashMap();
                map.put("viJsonParam", "{\"w\":\"admin\",\"d\":\"listSyncRes\",\"v\":\"1\"}");
                sessionTemp.update("syncToRead", map);
                if(Sessions.toInfo) {
                    this.log.info("listQueueSQL res-{}", map.get("result"));
                }

                ArrayList list = new ArrayList();
                list.add((String)map.get("voRes1"));
                list.add((String)map.get("voRes2"));
                list.add((String)map.get("voRes3"));
                list.add((String)map.get("voRes4"));
                list.add((String)map.get("voRes5"));
                list.add((String)map.get("voRes6"));
                list.add((String)map.get("voRes7"));
                list.add((String)map.get("voRes8"));
                list.add((String)map.get("voRes9"));

                String row;
                for(Iterator var8 = list.iterator(); var8.hasNext(); res = res + row) {
                    row = (String)var8.next();
                    if(row == null) {
                        row = "";
                    }
                }
            } catch (Exception var13) {
                if(Sessions.toError) {
                    this.log.error("listQueueSQL res-{}", var13.getMessage());
                }
            } finally {
                if(sessionTemp != null) {
                    sessionTemp.close();
                }

            }
        } catch (Exception var15) {
            ;
        }

        return res;
    }

    private String lpad(String s, int i, char c) {
        String res = s;

        for(int j = 0; j < i - s.length(); ++j) {
            res = c + res;
        }

        return res;
    }

    public String toExecuteSQL(String id, List<String> listSQL) {
        String res = "";

        try {
            SqlSession sessionTemp = null;

            try {
                SqlSessionFactory e = (SqlSessionFactory)this.sessionMap.get(id);
                sessionTemp = e.openSession();
                HashMap map = new HashMap();
                map.put("viToken", "17712613261");
                int iAdd = 0;
                Iterator var9 = listSQL.iterator();

                while(var9.hasNext()) {
                    String row = (String)var9.next();
                    ++iAdd;
                    map.put("vi" + this.lpad(String.valueOf(iAdd), 3, '0'), row);
                }

                sessionTemp.update("toExecuteSQL", map);
                res = (String)map.get("res");
                if(Sessions.toInfo) {
                    this.log.info("toExecuteSQL res-{}", res);
                }
            } catch (Exception var14) {
                if(Sessions.toError) {
                    this.log.error("toExecuteSQL res-{}", var14.getMessage());
                }
            } finally {
                if(sessionTemp != null) {
                    sessionTemp.close();
                }

            }
        } catch (Exception var16) {
            ;
        }

        return res;
    }

    public String toSelect(String id, String con) {
        StringBuffer res = new StringBuffer();

        try {
            SqlSession e = null;

            try {
//                SqlSessionFactory e1 = (SqlSessionFactory)this.sessionMap.get(id);
                e = MyBatisUtil.getSqlSession();
                HashMap map = new HashMap();
                map.put("viCon", con);
                e.update("toSelect", map);
                String tmp = "";

                for(int i = 1; i <= 200; ++i) {
                    tmp = (String)map.get("vo" + this.lpad(String.valueOf(i), 3, '0'));
                    if(tmp != null) {
                        res.append(tmp);
                    }
                }

                if(res.length() < 1) {
                    res.append("{\"res\":\"0\",\"msg\":\"" + (String)map.get("res") + "\"}");
                }

                if(Sessions.toInfo) {
                    this.log.info("toSelect res-{}", map.get("res"));
                }
            } catch (Exception var13) {
                var13.printStackTrace();
                if(Sessions.toError) {
                    this.log.error("toSelect res-{}", var13.getMessage());
                }

                res.append("{\"res\":\"0\",\"msg\":\"" + var13.getMessage() + "\"}");
            } finally {
                if(e != null) {
                    e.close();
                }

            }
        } catch (Exception var15) {
            res.append("{\"res\":\"0\",\"msg\":\"" + var15.getMessage() + "\"}");
        }

        this.log.info("存储过程调用结果->" + res.toString());
        return res.toString();
    }

}
